#include "board.h"
//#include "at32f403_board.h"

i2c_handle_type hi2cx;
void error_handler(uint32_t error_code)
{
	while(1)
	{
		led_r_on();
		delay_ms(500);
		led_r_off();
		delay_ms(500);
	}
}

bool iic_init(void)
{
	hi2cx.i2cx = I2Cx_PORT;
	i2c_config(&hi2cx);
	return true;
}

bool iic_read(uint8_t Address,uint8_t *dataRev,int num)
{
	i2c_status_type i2c_status;
	/* start the request reception process */
	if((i2c_status = i2c_master_receive_int(&hi2cx, I2Cx_ADDRESS, dataRev, num, I2C_TIMEOUT)) != I2C_OK)
	{
	  error_handler(i2c_status);
	}
	/* wait for the communication to end */
	if(i2c_wait_end(&hi2cx, I2C_TIMEOUT) != I2C_OK)
	{
	  error_handler(i2c_status);
	}
	return true;
}

bool iic_wirte(uint8_t Address,uint8_t *data,int num)
{
	i2c_status_type i2c_status;
	/* start the request reception process */
	if((i2c_status = i2c_master_transmit_int(&hi2cx, Address, data, num, I2C_TIMEOUT)) != I2C_OK)
	{
	  error_handler(i2c_status);
	}

	/* wait for the communication to end */
	if(i2c_wait_end(&hi2cx, I2C_TIMEOUT) != I2C_OK)
	{
	  error_handler(i2c_status);
	}
	return true;
}

